﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Screenr
{
    public class ScreenrConfigControl : AbstractConfigControl
    {
        private ScreenrConfig _config;
        private TextBox usernameTextBox = new TextBox();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                return new ScreenrConfig
                {
                    Username = usernameTextBox.Text
                }
                .SerializeToJson();
            }
        }

        public ScreenrConfigControl(string config)
        {
            _config = config.DeserializeJsonTo<ScreenrConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                usernameTextBox.Text = _config.Username;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            usernameTextBox.ID = "usernameTextBox";

            usernameTextBox.CssClass = "guiInputText guiInputStandardSize";

            Controls.Add(usernameTextBox);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("Username:", "Please enter your Screenr username", usernameTextBox);
        }
    }
}